#ifndef __SQLITE_BASE_H__
#define __SQLITE_BASE_H__

#include "sqlite3.h"

//#define SQLITE_DEBUG

#define SQLITE_DB_BUSY_TIMEOUT              (1100)

#define SQL_DROP_TABLE(sql, name) \
do {\
    snprintf(sql, sizeof(sql), "DROP TABLE %s", name); \
}while (0);

typedef int (*SQLITE_QUERY_FP)(unsigned char *, int, void *);
typedef sqlite3 DBHANDLE_s;

typedef enum __SQLITE_SORT_TYPE_e_
{
    SQLITE_SORT_TYPE_ASC = 1,
    SQLITE_SORT_TYPE_DESC,
}SQLITE_SORT_TYPE_e;

typedef struct _SQLITE_SORT_s_
{
    char *pcTableName;
    char *pcSortName;
    SQLITE_SORT_TYPE_e enType;
    SQLITE_QUERY_FP fpCallBack;
}SQLITE_SORT_s;

typedef struct _SQLITE_QUERY_s_
{
    char *pcSql;
    char **result;
    int iRow;
    int iCol;
}SQLITE_QUERY_s;

typedef struct _SQLITE_DELETE_OLD_s_
{
    char *pcTableName;
    char *pcSortColName;
    int iDelNum;
}SQLITE_DELETE_OLD_s;

typedef struct _SQLITE_UPDATE_BLOB_s_
{
    char *pcTableName;
    char *pcKeyColName;
    char *pcKeyColData;
    char *setColName;
    unsigned char *pucData;
    int iLen;
}SQLITE_UPDATE_BLOB_s;

typedef struct _SQLITE_DELETE_TABLE_DATA_s_
{
    char *pcTableName;
    char *pcColName;
    char *pcColData;
}SQLITE_DELETE_TABLE_DATA_s;

typedef struct _SQLITE_INSERT_BLOB_DATA_s__
{
    char *pcBlobData;
    int iBlobLen;
}SQLITE_INSERT_BLOB_DATA_s;

typedef struct _SQLITE_INSERT_BLOB_s__
{
    int iCount;
    SQLITE_INSERT_BLOB_DATA_s astData[0];
}SQLITE_INSERT_BLOB_s;

typedef struct __SQLITE_BASE_s__
{
    char *pcTable;
    char *pcPath;
    DBHANDLE_s *pstSqlDb;
    sqlite3_stmt *pStmt;
}SQLITE_BASE_s;

typedef int (*SQLITE_CHECK_TABLE_F)(DBHANDLE_s *, void *);

extern int Sqlite_OpenDB(char *pcDbName, DBHANDLE_s **ppstSqlDb);
extern int Sqlite_Exec(DBHANDLE_s *pstSqlDb, char *pcSql);
extern int Sqlite_Prepare(DBHANDLE_s *pstSqlDb, sqlite3_stmt **ppstmt, char *pcSql);
extern void Sqlite_FreeResult(char ***pstResult);
extern int Sqlite_QueryResult(DBHANDLE_s *pstSqlDb, SQLITE_QUERY_s *pstResult);
extern int Sqlite_SortTable( DBHANDLE_s *pstSqlDb, SQLITE_SORT_s stSort);
extern int Sqlite_PrintTable( DBHANDLE_s *pstSqlDb, char *pcTableName );
extern int Sqlite_deleteOld( DBHANDLE_s *pstSqlDb, SQLITE_DELETE_OLD_s stDelete);
extern int Sqlite_deleteNew( DBHANDLE_s *pstSqlDb, SQLITE_DELETE_OLD_s stDelete );
extern int Sqlite_deleteData( DBHANDLE_s *pstSqlDb, SQLITE_DELETE_TABLE_DATA_s stDelete );
extern void Sqlite_close( DBHANDLE_s **ppstSqlDb );
extern int Sqlite_getTotalNum( DBHANDLE_s *pstSqlDb, char *pcTableName, int *iNum );
extern int Sqlite_updateBlob( DBHANDLE_s *pstSqlDb, SQLITE_UPDATE_BLOB_s stBlob);
extern int Sqlite_QueryBlob( DBHANDLE_s *pstSqlDb, char *pcSql, SQLITE_QUERY_FP fCallBack, void *pParam);
extern int Sqlite_Insert( DBHANDLE_s *pstSqlDb, char *pcSql, SQLITE_INSERT_BLOB_s *pstBlob);
extern int Sqlite_GetColName(DBHANDLE_s *pstSqlDb, char *pcTableName, char ***pppcRes, int *piCol);
extern int Sqlite_CheckAndCreate(DBHANDLE_s *pstSqlDb, char *pcTableName, char *pcCreatTable);
extern int Sqlite_CheckDB(char *pcDbName, DBHANDLE_s **ppstSqlDb);
extern int Sqlite_SaveBackup(char *pcPath, sqlite3 *pmemoryDb);
extern int SqliteSql_Prepare(SQLITE_BASE_s *pstBase, SQLITE_CHECK_TABLE_F pfCheckCB, char *pcSql);
extern void SqliteSql_Close(SQLITE_BASE_s *pstBase);
extern int SqliteSql_OpenDB(SQLITE_BASE_s *pstBase, SQLITE_CHECK_TABLE_F pfCheckTableCB);
extern int SqliteSql_AffairsBegin(SQLITE_BASE_s *pstBase);
extern int SqliteSql_AffairsCommit(SQLITE_BASE_s *pstBase);
extern int SqliteSql_AffairsRollBack(SQLITE_BASE_s *pstBase);

#endif

